<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">考试管理</el-breadcrumb-item>
      <el-breadcrumb-item>添加考试</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>
    <div style="width: 500px;margin: 0 auto">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="考试名称">
          <el-input v-model="ruleForm.title"></el-input>
        </el-form-item>
        <el-form-item label="考试描述">
          <el-input v-model="ruleForm.description"></el-input>
        </el-form-item>
        <el-form-item label="报名开始时间">
          <div class="block">
            <el-date-picker
                value-format="yyyy-MM-dd HH:mm:ss"
                v-model="ruleForm.startTime"
                type="datetime"
                placeholder="选择日期时间">
            </el-date-picker>
          </div>
        </el-form-item>
        <el-form-item label="报名结束时间">
          <div class="block">
            <el-date-picker
                value-format="yyyy-MM-dd HH:mm:ss"
                v-model="ruleForm.endTime"
                type="datetime"
                placeholder="选择日期时间">
            </el-date-picker>
          </div>
        </el-form-item>
        <el-form-item label="考试费用">
          <el-input v-model="ruleForm.price"></el-input>
        </el-form-item>
        <el-form-item label="考试开始时间">
          <div class="block">
            <el-date-picker
                value-format="yyyy-MM-dd HH:mm:ss"
                v-model="ruleForm.applyStartTime"
                type="datetime"
                placeholder="选择日期时间">
            </el-date-picker>
          </div>
        </el-form-item>
        <el-form-item label="考试结束时间">
          <div class="block">
            <el-date-picker
                value-format="yyyy-MM-dd HH:mm:ss"
                v-model="ruleForm.applyEndTime"
                type="datetime"
                placeholder="选择日期时间">
            </el-date-picker>
          </div>
        </el-form-item>
        <el-form-item label="注意事项">
          <el-input v-model="ruleForm.announcements"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button plain type="primary" @click="submitForm('ruleForm')">添加</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<style>

</style>
<script>

export default {

  data() {
    return {

      ruleForm: {
        title :'',
        description: '',
        startTime:'',
        endTime: '',
        price: '',
        applyStartTime: '',
        applyEndTime: '',
        announcements: '',
      },
      rules: {
        /*trigger: 'blur'触发方式,丢失焦点*/
        title: [
          { required: true, message: '请输入名称', trigger: 'blur' },
          { min: 2, max: 15, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入简介', trigger: 'blur' },
          { min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur' }
        ],
        price: [
          { required: true, message: '请输入价格', trigger: 'blur' },
          { min: 1, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur' }
        ],
        announcements: [
          { required: true, message: '请输入注意事项', trigger: 'blur' },
          { min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur' }
        ],
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          /*向后端提交异步请求*/
          let  url="http://120.77.154.166:8080/exam/add-new"

          console.log(this.ruleForm);
          let formData = this.qs.stringify(this.ruleForm);
          console.log('formData = ' + formData);
          this.axios
              .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
              .post(url,formData).then((response)=>{
            let jsonResult = response.data;
            if (jsonResult.state == 20000) {
              this.$notify({
                title: '成功',
                message: jsonResult.message,
                type: 'success'
              });
              this.resetForm(formName)
            }else {
              this.$notify.error({
                title: '警告',
                message: jsonResult.message,
              });
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>
